{% extends 'base_children.html' %}

{% block self_head %}
    {##这里是 个性化的头部脚本呢#}
{% endblock %}

{% block content %}
    {#    #这里是真正的内容部分#}

    <div class="layui-fluid">
        <div class="layui-row  layui-col-space15 larryms-count-demo">

            <div class="layui-col-xs12 layui-col-sm12 layui-col-md10 layui-col-lg10">
                <section class="layui-card">


                    <div class="layui-card-body">
                        <blockquote class="layui-elem-quote">

                            <div class="layui-btn-group larryms-btn-group">
                                <div class="layui-inline">
                                    <div class="margin-15 layui-show-xs layui-hide-sm layui-hide-md layui-hide-lg"></div>
                                    <button class="layui-btn layui-btn-sm layui-btn-danger" data-type="del"  data-url="{% url 'cmdb_ip_api' %}"><i class="layui-icon">&#xe640;</i>
                                <cite>删除ip</cite>
                            </button>
                                    <button class="layui-btn layui-btn-sm layui-btn-normal" data-type="scan"
                                            data-url="{% url 'cmdb_scan_ip' %}"><i class="layui-icon">&#xe61f;</i>
                                        <cite>扫描</cite>
                                    </button>
                                </div>
                            </div>
                        </blockquote>
                          <table id="demo" lay-filter="demo" class="larryms-table-id" data-url="{%  url 'cmdb_ip_api' %}"></table>
                    </div>
                </section>
            </div>


        </div>

    </div>
    <script type="text/html" id="demoBtnBar">
        <a class="layui-btn layui-btn-xs" data-url="{% url 'cmdb_scan_ip_edit' %}" lay-event="edit">编辑</a>
{#        <a class="layui-btn layui-btn-xs layui-btn-danger" data-url="" lay-event="del">删除</a>#}
    </script>



{% endblock %}

{% block self_foot %}
    {#    #这里是个性化的底足脚本#}

    <script type="text/javascript">
        layui.config({
            version: "2.0.8",
            base: '/static/larryms/',
            layertype: 2, //iframe内层弹窗类型不显示返回首页
            rightMenu: false // false关闭，设置为custom时使用自定义，不使用默认menu
        }).extend({
            larry: 'js/base'
        }).use(['jquery', 'table', 'form', 'layer', 'larry'], function () {
            var $ = layui.$,
                larryms = layui.larryms,
                form = layui.form;
            table = layui.table;
            layer = layui.layer;
            //按钮监听
            $(' .layui-btn').on('click', function () {
                var type = $(this).data('type'),
                    url = $(this).data('url'),
                    tit = $(this).children('cite').text();
                active[type] ? active[type].call(this, url, tit) : '';
            });
            var active = {
                scan: function(){
                    var url = $(this).data('url');
                    larryms.confirm(' 开始扫描网络，请稍后刷新本页面？', {icon: 3, title:'提示！'},function () {
                        console.log(url)
                        $.ajax({
                            url:url, //请求地址
                            type:"POST",  //提交类似//提交参数
                            dataType: "JSON",
                            data:{'id':'1,2,4,5'},
                            success: function (res) {
                                larryms.msg(res.msg)
                                table.reload('demo', {})
                                console.log(res)
                                {#larryms.confirm(res.responseText)#}
                                {#table.reload('demo', {})#}
                            },error:function (res) {
                                larryms.confirm(res.responseText);
                            }
                        });
                    });


                },
             del: function() {
        //批量删除
          checkStatus = table.checkStatus('demo'),
          data = checkStatus.data,
          newsId = [];
          console.log(data);
         var url = $(this).data('url');
        console.log(url);
        if (data.length > 0) {
          for (var i in data) {
            //排除默认管理员
                newsId.push(data[i].id);
          }
          if(newsId.length>0){
               var ids = {"id":newsId };
              console.log(newsId);
              larryms.confirm('你确定要执行批量删除吗？',{icon: 3, title:'批量删除提示！'},function(){
                  $.ajax({
			    dataType: "JSON",
			    url:url, //请求地址
			    type:"DELETE",  //提交类似
			    contentType: "application/json",
			    data: JSON.stringify({"id":newsId }),  //提交参数
			    success:function(res){
				larryms.msg(res.msg)
                    table.reload('demo', {})
			},
	    	error:function(response){
				larryms.msg( response.responseText);
	    	}
		});
              });
          }else{
              larryms.msg('默认管理员不可删除,我是神一样的存在！');
          }
        } else {
          larryms.msg('请至少选择一项，进行删除操作');
        }
      }
            };

 // 来自角色用户组
        var tableDataUrl = $('#demo').data('url'),
            roleIndex = table.render({
                 elem:'#demo',
                 id:'demo',
                 cellMinWidth:80,
                 url:tableDataUrl,
                 method:'get',
                 height:'full-130',
                 page:true,
                 cols:[
                   [
                       {type: "checkbox", fixed: 'left', width: 50},
                       {field: 'ip', title: 'IP', minWidth: 180, align: 'center'},
                       {field: 'subnet', title: '子网', minWidth:180, align: 'center'},
                        {field: 'beizhu', title: '备注', minWidth: 180, align: 'center'},
                       {title: '操作', minWidth: 35, templet: '#demoBtnBar', fixed: "right", align: "center"}
                       ]
                 ]
            });

                  // 监听工具条
var pageTableID = $('.larryms-table-id').attr('id');
table.on('tool('+pageTableID+')', function(obj) {
      var data = obj.data, tit = '编辑 <em class="tit">'+data.name+ '</em> 信息';
      console.log(data)
      if(obj.event == 'edit'){
          var url =  $(this).data('url') +'?id='+data.id;
          var editIndex =  larryms.open({
              title: tit,
              type: 2,
              area: ['460px', '450px'],
              content: url,
              success: function(index, layero) {
                form.render();
              }
          });
      }else if(obj.event == 'detail'){
          var url = $(this).data('url');
          larryms.confirm('查看详情还没开始作？',{icon: 3, title:'删除提示'},function(){
              var ids = {"id":data.id};
              $.post(url,ids,function(res){
                  if(res.code==200){
                      larryms.msg(res.msg);
                      table.reload(pageTableID,{});
                  }else{
                     larryms.msg(res.msg);
                  }
              });
          });
      }else if(obj.event == 'del'){
          var url = $(this).data('url');
           larryms.confirm('查看详情还没开始作？',{icon: 3, title:'删除提示'},function(){
              var ids = {"id":[data.id]};
              $.post(url,ids,function(res){
                  if(res.code==200){
                      larryms.msg(res.msg);
                      table.reload(pageTableID,{});
                  }else{
                     larryms.msg(res.msg);
                  }
              });
      });
}})

        });
    </script>


    <script>
        function tanchu(n) {
            layer.open({
                type: 2,
                title: '修改主机信息',
                closeBtn: 1,
                area: ['700px', '550px'],
                shadeClose: true, //点击遮罩关闭
                content: ['/cmdb/ipasset/edit/' + n,],
                end: function () {
                    var handle_status = $("#handle_status").val();
                    if (handle_status == '1') {
                        layer.msg('保存成功！', {
                            icon: 1,
                            time: 2000 //2秒关闭（如果不配置，默认是3秒）
                        }, function () {
                            history.go(0);
                        });
                    } else if (handle_status == '2') {
                        layer.msg('修改失败！', {
                            icon: 2,
                            time: 2000 //2秒关闭（如果不配置，默认是3秒）
                        }, function () {
                            history.go(0);
                        });
                    }
                }
            });
        }
    </script>
{% endblock %}


